﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using tmAsk.Common;

namespace tmAsk.Manager
{
    public partial class Admin_seting : System.Web.UI.Page
    {
        function make_htm = new function();//继承类
        tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();
        protected void Page_Load(object sender, EventArgs e)
        {
            function.CheckAdminLogin();
            string T = Request.QueryString["T"];
            switch (T)
            {
                case "click":
                    GetClick();
                    break;
                case "all":
                    GetAll();
                    break;
                case "question":
                    GetQuestionHtml();
                    break;
                case "news":
                    GetNewsHtml();
                    break;
                case "help":
                    GetHelpHtml();
                    break;
            }
            Response.Write("<script>alert('生成成功');location='admin_url.aspx?template=template/t_admin_set.html';</script>");
        }

        private void GetClick()
        {
            string m_id = Request["m_id"];

            DataTable rs = null;
            string html = "";
            string template_url = "";
            string makehtml = "";
            if (m_id != null && m_id != "")
            {
                rs = SQL.GetList("B_makehtml", "m_id=" + m_id, "*").Tables[0];

                for (int i = 0; i < rs.Rows.Count; i++)
                {
                    template_url = rs.Rows[i]["m_template"].ToString();//模板名称
                    makehtml = rs.Rows[i]["m_url"].ToString();//生成模板地址
                    // try
                    // {
                    html = make_htm.Get_html(HttpContext.Current.Server.MapPath(@"~/") + template_url, HttpContext.Current.Server.MapPath(@"~/") + makehtml);
                    //}
                    //catch (Exception e)
                    //{
                    //    Response.Write("<script>alert('"+e.Message+"');window.history.go(-1);</script>");
                    //    return;
                    //}
                }
            }
        }

        private void GetAll()
        {
            DataTable rs = null;
            string html = "";
            string template_url = "";
            string makehtml = "";

            rs = SQL.GetList("B_makehtml", "1=1", "*").Tables[0];
            for (int i = 0; i < rs.Rows.Count; i++)
            {
                template_url = rs.Rows[i]["m_template"].ToString();//模板名称
                makehtml = rs.Rows[i]["m_url"].ToString();//生成模板地址
                try
                {
                    html = make_htm.Get_html(HttpContext.Current.Server.MapPath(@"~/") + template_url, HttpContext.Current.Server.MapPath(@"~/") + makehtml);
                }
                catch
                {
                    Response.Write("<script>alert('生成失败，请检查 " + template_url + " 的路径是否正确！');window.history.go(-1);</script>");
                    return;
                }
            }
        }

        private void GetHelpHtml()
        {
            DataTable rs = SQL.GetList("b_help", "", "h_id").Tables[0];
            for (int i = 0; i < rs.Rows.Count; i++)
            {
                string id = rs.Rows[i]["h_id"].ToString();

                string html = "";

                string template_url = "template/m_help.html";//模板名称

                string makehtml = "Help/A_help_" + id + ".html";//生成模板地址

                html = make_htm.Get_html(HttpContext.Current.Server.MapPath(@"~/") + template_url, HttpContext.Current.Server.MapPath(@"~/") + makehtml, "[id]", id);
            }
        }

        private void GetNewsHtml()
        {
            DataTable rs = SQL.GetList("b_news", "", "n_id").Tables[0];
            for (int i = 0; i < rs.Rows.Count; i++)
            {
                string id = rs.Rows[i]["n_id"].ToString();

                string html = "";

                string template_url = "template/m_gonggao.html";//模板名称

                string makehtml = "News/A_notice_" + id + ".html";//生成模板地址

                html = make_htm.Get_html(HttpContext.Current.Server.MapPath(@"~/") + template_url, HttpContext.Current.Server.MapPath(@"~/") + makehtml, "[id]", id);
            }
        }

        private void GetQuestionHtml()
        {
            DataTable rs = SQL.GetList("B_ask", "", "a_id").Tables[0];
            for (int i = 0; i < rs.Rows.Count; i++)
            {
                string id = rs.Rows[i]["a_id"].ToString();

                string html = "";

                string template_url = "template/m_neirong.html";//模板名称

                string makehtml = "Ask/A_" + make_htm.Md5(id) + ".html";//生成模板地址

                html = make_htm.Get_html(HttpContext.Current.Server.MapPath(@"~/") + template_url, HttpContext.Current.Server.MapPath(@"~/") + makehtml, "[id]", id);
            }
        }
    }
}